package com.itheima.stock.handler;


import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;
import com.itheima.stock.exception.StockException;
import com.itheima.stock.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = StockException.class)
    public ResponseResult<?> handleStockException(StockException e) {
        return ResponseResult.error(e.getCode());
    }
    /**
     * 处理未知异常
     * @param e
     * @return
     */
    @SuppressWarnings("all")
    @ExceptionHandler(value = Exception.class)
    public ResponseResult<?> handleUnKnownException(Exception e) {
        log.error("系统异常,",e);
        //TODO
        e.printStackTrace();
        return ResponseResult.error(CodeEnum.SYSTEM_ERROR);
    }
}
